<?php

class UrBookLibController extends Controller
{
	
	public function filters()
    {
        return array('accessControl');
    }
	public function accessRules()
    {
        return array(
        		array('allow',
        			'actions'=>array('bookLibBox'),
        			'users'=>array('*'),
        		),
            array('allow',
                'actions'=>array('add','delete'),
                'users'=>array('@'),
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }
    
	public function actionAdd()
	{
		$this->render('add');
	}

	public function actionDelete()
	{
		$this->render('delete');
	}
	
	/**
	 * 选择图书馆收藏
	 * 传值方式 ajx
	 * step＝1	选择与本人相关的图书馆
	 * step＝2	点击确认	
	 */
	public function actionBookLibBox(){
		if(user()->isGuest){
			 echo 'guest';
			 exit;
		}
		$step = getPost('step');
		if($step == 1){
			$bid = getPost('bid');
			$html = '';
			$liblist = UrMemberLib::model()->findAll('uid=:uid',array(':uid'=>user()->id));
	
			$arrDropdown = array();

			foreach($liblist as $k=>$v){
				$key = $v->lid;
				$arrDropdown[$key] = $v->lib->name;
				}
			if($liblist===null){
				$html .= "你还没有选择你周边的图书馆,请在首页添加周边图书馆后再进行操作";
				}
				else{
				$html .= CHtml::dropDownList('libid','',$arrDropdown);	
				$html .= CHtml::button('提交',array('onclick'=>"$.ajax({
					url:'".$this->createUrl('/urBookLib/bookLibBox')."',
					type:'POST',
					data:'lid='+$('#libid').val()+'&step=2&bid=".$bid."',
					success:function(data){
						if(data==1){
							window.location.reload();
							}
						}
					})"));
				}
				echo $html;exit;
			}

			if($step==2){
				$lid = getPost('lid');
				$bid = getPost('bid');
			
				$urBookLib = new UrBookLib;
				$urBookLib->uid = user()->id;
				$urBookLib->bid = $bid;
				$urBookLib->lid = $lid;
				$urBookLib->create_datetime = time();
				$urBookLib->update_datetime = time();
				if($urBookLib->save()){
					echo 1;
					}
				}
		}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}
